﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Mvc;
using ExcellOnServices.Models;
using PagedList;
using ExcellOnServices.Models.Utils;

namespace ExcellOnServices.Areas.Admin.Controllers
{
    public class ProductController : Controller
    {
        private ExcellOnServicesContext db = new ExcellOnServicesContext();

        // GET: /Admin/Product/
        [Authorize]
        public ActionResult Index(string search, string sortBy = "publish", bool sortAsce = true, int? category = null, int? company = null, int page = 1)
        {
            var products = db.Products.Include(p => p.Accounts_Company).Include(p => p.Category);

            if (!String.IsNullOrEmpty(search))
            {
                products = products.Where(s => s.Name.ToUpper().Contains(search.ToUpper()));
            }

            if (category != null)
            {
                products = products.Where(s => s.Category_Id == category);
            }

            if (company != null)
            {
                products = products.Where(s => s.Company_Id == company);
            }

            switch (sortBy)
            {
                case "brandname":
                    if (!sortAsce)
                    {
                        products = products.OrderByDescending(b => b.Accounts_Company.BrandName);
                    }
                    else
                    {
                        products = products.OrderBy(b => b.Accounts_Company.BrandName);
                    }
                    break;
                case "category":
                    if (!sortAsce)
                    {
                        products = products.OrderByDescending(b => b.Category.Name);
                    }
                    else
                    {
                        products = products.OrderBy(b => b.Category.Name);
                    }
                    break;
                case "name":
                    if (!sortAsce)
                    {
                        products = products.OrderByDescending(b => b.Name);
                    }
                    else
                    {
                        products = products.OrderBy(b => b.Name);
                    }
                    break;
                case "price":
                    if (!sortAsce)
                    {
                        products = products.OrderByDescending(b => b.Price);
                    }
                    else
                    {
                        products = products.OrderBy(b => b.Price);
                    }
                    break;
                case "discount":
                    if (!sortAsce)
                    {
                        products = products.OrderByDescending(b => b.Discount);
                    }
                    else
                    {
                        products = products.OrderBy(b => b.Discount);
                    }
                    break;
                case "publish":
                    if (!sortAsce)
                    {
                        products = products.OrderByDescending(b => b.Published);
                    }
                    else
                    {
                        products = products.OrderBy(b => b.Published);
                    }
                    break;
            }

            ViewBag.SortBy = sortBy;
            ViewBag.SortOrder = sortAsce;
            ViewBag.CategoryId = category;
            ViewBag.CompanyId = company;
            ViewBag.SearchString = search;
            ViewBag.Page = page;
            ViewBag.Category = new SelectList(db.Categories.ToList(), "Id", "Name");
            ViewBag.Company = new SelectList(db.Accounts_Company.ToList(), "Id", "BrandName");
            return View(products.ToPagedList(page, 10));
        }

        [Authorize]
        public ActionResult SetUnpublish(int id, string search, int? category, int? company, string sortBy = "price", bool sortAsce = true)
        {
            Product p = db.Products.Find(id);
            p.Published = !p.Published;
            db.SaveChanges();

            return RedirectToAction("Index", new { search = search, sortBy = sortBy, sortAsce = sortAsce, category = category, company = company });
        }

        // GET: /Admin/Product/Details/5
        [Authorize]
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Product product = db.Products.Find(id);
            if (product == null)
            {
                return HttpNotFound();
            }
            return View(product);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
